package com.yhh.Entity;

import com.yhh.Exceptions.InstructionException;
import com.yhh.myUtils.myStringUtils;

import java.text.DateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class MyFileFactory {
    public static DateFormat dfd = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
    public static DateFormat dft = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CHINA);

    /**
     * 根据传入的指令处理file的相关设置
     * @param args
     * @return
     * @throws InstructionException
     * @throws NumberFormatException
     */
    public static MyFile executeSend(Map<String, String> args) throws InstructionException,NumberFormatException {
        MyFile file = new MyFile();
        //如果存在-d指令，
        if (args.get("-d") != null) {
            int counts = Integer.parseInt(args.get("-d"));
            file.setCounts(Math.max(counts, 1));
        }
        if (args.get("-t")!=null){
            List<String> time = myStringUtils.getTimeUnit(args.get("-t"));
            int ttl = Integer.parseInt(time.get(0));
            file.setTtl(ttl);
            String timeUnit = time.get(1);
            if("SECONDS".equalsIgnoreCase(timeUnit)||"sec".equalsIgnoreCase(timeUnit)||"s".equalsIgnoreCase(timeUnit)) file.setTimeUnit(TimeUnit.SECONDS);
            else if("DAYS".equalsIgnoreCase(timeUnit)||"d".equalsIgnoreCase(timeUnit)) file.setTimeUnit(TimeUnit.DAYS);
            else if("HOURS".equalsIgnoreCase(timeUnit)||"h".equalsIgnoreCase(timeUnit)) file.setTimeUnit(TimeUnit.HOURS);
            else if("MINUTES".equalsIgnoreCase(timeUnit)||"min".equalsIgnoreCase(timeUnit)||"m".equalsIgnoreCase(timeUnit)) file.setTimeUnit(TimeUnit.MINUTES);
            else throw new InstructionException("未知的时间参数","-t");
        }
        if (args.get("-m")!=null){
            file.setContent(args.get("-m"));
        }
        return file;
    }
}
